package com.redsoil.house.ui.capital.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnClickListener;
import com.orhanobut.dialogplus.ViewHolder;
import com.redsoil.house.R;
import com.redsoil.house.base.RootFragment;
import com.redsoil.house.model.bean.PropertyListVO;
import com.redsoil.house.model.bean.TestVO;
import com.redsoil.house.ui.capital.adapter.CapitalPropertyAdapter;
import com.redsoil.house.ui.capital.contract.CapitalPropertyListContract;
import com.redsoil.house.ui.capital.presenter.CapitalPropertyListPresenter;
import com.redsoil.house.ui.property.activity.PropertyActivity;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 物业管理
 */
public class CapitalPropertyListFragment extends RootFragment<CapitalPropertyListPresenter> implements CapitalPropertyListContract.View {

    @BindView(R.id.rv_list)
    RecyclerView rv_list;

    @BindView(R.id.build_filter_layout)
    AutoLinearLayout build_filter_layout;

    @BindView(R.id.building_tv)
    TextView building_tv;

    //物业类型：BUILD 一栋/HOUSE 公寓
    private static final String BUILD = "BUILD";
    private static final String HOUSE = "HOUSE";

    private String typeCode = HOUSE;

    private DialogPlus filterDialog;

    private CapitalPropertyAdapter capitalPropertyAdapter;

    private List<PropertyListVO.DataListBean> datas;

    public static CapitalPropertyListFragment newInstance() {
        Bundle args = new Bundle();

        CapitalPropertyListFragment fragment = new CapitalPropertyListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initEventAndData() {
        super.initEventAndData();

        initAdapter();

        getPropertyList();
    }

    private void initAdapter(){
        rv_list.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @OnClick(R.id.build_filter_layout)
    void onBuildFilterLayout(){
        if (filterDialog == null) {
            filterDialog = DialogPlus.newDialog(getContext())
                    .setGravity(Gravity.BOTTOM)
                    .setContentHolder(new ViewHolder(R.layout.capital_property_filter_dialog))
//                    .setContentBackgroundResource(R.drawable.dialog_write_corner_bg)
                    .setCancelable(false)//设置不可取消   可以取消
                    .setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(DialogPlus dialog, View view) {
                            switch (view.getId()) {
                                case R.id.capital_build_tv:
                                    building_tv.setText(getString(R.string.building));
                                    if(!typeCode.equals(HOUSE)){
                                        typeCode = HOUSE;
                                        getPropertyList();
                                    }
                                    dialog.dismiss();
                                    break;
                                case R.id.capital_one_build_tv:
                                    building_tv.setText(getString(R.string.one_building));
                                    if(!typeCode.equals(BUILD)){
                                        typeCode = BUILD;
                                        getPropertyList();
                                    }
                                    dialog.dismiss();
                                    break;
                                case R.id.capital_cancel_tv:
                                    dialog.dismiss();
                                    break;
                            }
                        }
                    })
                    .create();

        }

        filterDialog.show();
    }

    private void getPropertyList(){
        //请求数据
        stateLoading();
        mPresenter.getPropertyList(typeCode);
    }

    @Override
    public void getPropertyListSuccess(PropertyListVO propertyListVO) {
        if(propertyListVO.getDataList().size() == 0){
            stateEmpty();
        }
        else {
            if(capitalPropertyAdapter == null){
                capitalPropertyAdapter = new CapitalPropertyAdapter(R.layout.capital_property_list_item,propertyListVO.getDataList());

                capitalPropertyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        PropertyListVO.DataListBean data = (PropertyListVO.DataListBean) adapter.getData().get(position);
                        getContext().startActivity(PropertyActivity.newInstance(getContext(),data.getId()));
                    }
                });

                rv_list.setAdapter(capitalPropertyAdapter);
            }else {
                rv_list.smoothScrollToPosition(0);
                capitalPropertyAdapter.setNewData(propertyListVO.getDataList());
            }

            stateMain();
        }
    }

    @Override
    public void responeError(String errorMsg) {
        stateError();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_capital_property_list;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }
}
